#include "settings.h"
#include "ui_settings.h"

#include <QSettings>

#include "gameapplication.h"
#include <mlgesture.h>

using namespace mlgesture;

Settings::Settings(QWidget *parent) :
    QDialog(parent),
	ui(new Ui::Settings)
{
    ui->setupUi(this);
	loadSettings();
	connect(ui->skinButton, SIGNAL(clicked()), this, SLOT(showSkinDialog()));
	skinDialog = NULL;
}

Settings::~Settings()
{
    delete ui;
	if (skinDialog) {
		delete skinDialog;
	}
}

void Settings::accept() {
	if (saveSettings())
		if (skinDialog) {
			delete skinDialog;
			skinDialog = NULL;
		}
		QDialog::accept();
}

void Settings::reject() {
	if (skinDialog) {
		delete skinDialog;
		skinDialog = NULL;
	}
	QDialog::reject();
}

void Settings::loadSettings() {
	QSettings settings;
	settings.beginGroup(QString("camera"));
	ui->camIdEdit->setText(settings.value("camId", "-1").toString());
	settings.endGroup();
	settings.beginGroup(QString("skinThreshold"));
	ui->lowSkin->setText(settings.value("lowHue", MLGesture::getGlobalLowThreshold()*2).toString());
	ui->upperSkin->setText(settings.value("upperHue", MLGesture::getGlobalUpperThreshold()*2).toString());
	ui->lowValue->setText(settings.value("lowValue", MLGesture::getGlobalValueLowThreshold()).toString());
	ui->upperValue->setText(settings.value("upperValue", MLGesture::getGlobalValueUpperThreshold()).toString());
	ui->adaptBox->setChecked(settings.value("adaptThresholding", 1).toInt() ? true : false);
	settings.endGroup();
	settings.beginGroup(QString("tracking"));
	ui->sensitySlider->setValue(settings.value("sensity", "0.9").toDouble() * 100);
	settings.endGroup();
	settings.beginGroup(QString("additionaWindows"));
	ui->showImageBox->setChecked(settings.value("showImage", 1).toInt());
	ui->showMaskBox->setChecked(settings.value("showMask", 1).toBool());
	settings.endGroup();
}

bool Settings::saveSettings() {

	QString camId =  ui->camIdEdit->text();
	bool ok;
	camId.toInt(&ok);
	if (!ok) {
		ui->errorLabel->setText(tr("Identyfikator kamery musi być liczbą"));
		QPalette p;
		p.setColor(QPalette::Text, Qt::red);
		ui->camIdEdit->setPalette(p);
		return false;
	}

	QSettings settings;
	settings.beginGroup(QString("camera"));
	settings.setValue(QString("camId"), camId);
	settings.endGroup();
	settings.beginGroup(QString("skinThreshold"));
	settings.setValue(QString("lowHue"), ui->lowSkin->text().toInt());
	settings.setValue(QString("upperHue"), ui->upperSkin->text().toInt());
	settings.setValue(QString("lowValue"), ui->lowValue->text().toInt());
	settings.setValue(QString("upperValue"), ui->upperValue->text().toInt());
	settings.setValue(QString("adaptThresholding"), ui->adaptBox->isChecked() ? 1 : 0);
	settings.endGroup();
	settings.beginGroup(QString("tracking"));
	settings.setValue(QString("sensity"), ui->sensitySpin->value() * 1. / 100);
	settings.endGroup();
	settings.beginGroup(QString("additionaWindows"));
	settings.setValue("showImage", ui->showImageBox->isChecked() ? 1 : 0);
	settings.setValue("showMask", ui->showMaskBox->isChecked() ? 1 : 0);
	settings.endGroup();

	GameApplication::instance()->loadSettings();
	return true;
}

void Settings::showSkinDialog() {
	if (!skinDialog) {
		skinDialog = new SkinThresholdDialog(ui->camIdEdit->text().toInt(), ui->lowSkin->text().toInt(), ui->upperSkin->text().toInt(),
											 ui->lowValue->text().toInt(), ui->upperValue->text().toInt(), this);
		connect(skinDialog, SIGNAL(accepted()), this, SLOT(saveSkin()));
	}
	skinDialog->show();
}

void Settings::saveSkin() {
	if (skinDialog) {
		ui->lowSkin->setText(QString::number(skinDialog->getLowThreshold()));
		ui->upperSkin->setText(QString::number(skinDialog->getUpperThreshold()));
		ui->lowValue->setText(QString::number(skinDialog->getValueLowThreshold()));
		ui->upperValue->setText(QString::number(skinDialog->getValueUpperThreshold()));
	}
}
